package com.retail.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.retail.common.result.Result;
import com.retail.user.dto.UserCreateDTO;
import com.retail.user.dto.UserQueryDTO;
import com.retail.user.dto.UserUpdateDTO;
import com.retail.user.dto.UserVO;
import com.retail.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.Map;

/**
 * 用户管理控制器
 * 
 * @author retail-platform
 * @since 2024-09-25
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/users")
@RequiredArgsConstructor
@Validated
public class UserController {

    private final UserService userService;

    /**
     * 分页查询用户列表
     */
    @GetMapping
    public Result<IPage<UserVO>> getUserPage(@Valid UserQueryDTO queryDTO) {
        log.info("分页查询用户列表请求 - 查询条件: {}", queryDTO);
        
        IPage<UserVO> result = userService.getUserPage(queryDTO);
        
        log.info("分页查询用户列表响应: {}", result);
        return Result.success(result);
    }

    /**
     * 根据用户ID查询用户详情
     */
    @GetMapping("/{id}")
    public Result<UserVO> getUserById(@PathVariable @NotNull Long id) {
        log.info("根据用户ID查询用户详情请求 - 用户ID: {}", id);
        
        UserVO result = userService.getUserById(id);
        
        log.info("根据用户ID查询用户详情响应: {}", result);
        return Result.success(result);
    }

    /**
     * 创建用户
     */
    @PostMapping
    public Result<Long> createUser(@RequestBody @Valid UserCreateDTO createDTO) {
        log.info("创建用户请求 - 用户信息: {}", createDTO);
        
        Long userId = userService.createUser(createDTO);
        
        log.info("创建用户响应 - 用户ID: {}", userId);
        return Result.success(userId);
    }

    /**
     * 更新用户
     */
    @PutMapping("/{id}")
    public Result<Boolean> updateUser(@PathVariable @NotNull Long id, 
                                    @RequestBody @Valid UserUpdateDTO updateDTO) {
        log.info("更新用户请求 - 用户ID: {}, 用户信息: {}", id, updateDTO);
        
        updateDTO.setId(id);
        boolean result = userService.updateUser(updateDTO);
        
        log.info("更新用户响应 - 用户ID: {}, 结果: {}", id, result);
        return Result.success(result);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteUser(@PathVariable @NotNull Long id) {
        log.info("删除用户请求 - 用户ID: {}", id);
        
        boolean result = userService.deleteUser(id);
        
        log.info("删除用户响应 - 用户ID: {}, 结果: {}", id, result);
        return Result.success(result);
    }

    /**
     * 重置用户密码
     */
    @PostMapping("/{id}/reset-password")
    public Result<Boolean> resetPassword(@PathVariable @NotNull Long id, 
                                       @RequestBody Map<String, String> request) {
        log.info("重置用户密码请求 - 用户ID: {}", id);
        
        String newPassword = request.get("newPassword");
        if (newPassword == null || newPassword.trim().isEmpty()) {
            return Result.error("新密码不能为空");
        }
        
        boolean result = userService.resetPassword(id, newPassword);
        
        log.info("重置用户密码响应 - 用户ID: {}, 结果: {}", id, result);
        return Result.success(result);
    }

    /**
     * 检查用户名是否存在
     */
    @GetMapping("/check-username")
    public Result<Boolean> checkUsername(@RequestParam String username, 
                                       @RequestParam(required = false) Long excludeUserId) {
        log.info("检查用户名是否存在请求 - 用户名: {}, 排除用户ID: {}", username, excludeUserId);
        
        boolean exists = userService.isUsernameExists(username, excludeUserId);
        
        log.info("检查用户名是否存在响应 - 用户名: {}, 存在: {}", username, exists);
        return Result.success(exists);
    }

    /**
     * 检查邮箱是否存在
     */
    @GetMapping("/check-email")
    public Result<Boolean> checkEmail(@RequestParam String email, 
                                    @RequestParam(required = false) Long excludeUserId) {
        log.info("检查邮箱是否存在请求 - 邮箱: {}, 排除用户ID: {}", email, excludeUserId);
        
        boolean exists = userService.isEmailExists(email, excludeUserId);
        
        log.info("检查邮箱是否存在响应 - 邮箱: {}, 存在: {}", email, exists);
        return Result.success(exists);
    }

    /**
     * 检查手机号是否存在
     */
    @GetMapping("/check-phone")
    public Result<Boolean> checkPhone(@RequestParam String phone, 
                                    @RequestParam(required = false) Long excludeUserId) {
        log.info("检查手机号是否存在请求 - 手机号: {}, 排除用户ID: {}", phone, excludeUserId);
        
        boolean exists = userService.isPhoneExists(phone, excludeUserId);
        
        log.info("检查手机号是否存在响应 - 手机号: {}, 存在: {}", phone, exists);
        return Result.success(exists);
    }
}
